Ext.ns("CRM.SPComp");

(function() {
  
	var win;
	var uploadDocStore=CRM.commons.createStore('uploadDocStore',CRM.SPComp.uploadDocDataArray,'documents/docList');
	
	CRM.SPComp.createDocumentUploader = function(){
		
		var detailsPanel = {
				xtype	: 'fieldset',
				id		: 'detailsPanel',
				title	: 'Document Details',
				autoHeight: true,
				width	: 600,
				layout	: 'form',
				items	: [
							   {
									xtype	  : 'textfield',
									inputType : 'file',
									id		  : 'file',
									fieldLabel: 'Choose Doc.'
							   },
					     	   {
					     	   		xtype	  : 'datefield',
					     	   		id		  : 'receiveDate',
					     	   		fieldLabel: 'Receiving Date'
					     	   },
					     	   {
					     		   	xtype	: 'textfield',
					     		   	id		: 'documentName',
					     		   	fieldLabel: 'Document Name',
					     		   	width	: 200
					     	   },
					     	   {
					     		   	xtype	: 'textfield',
					     		   	id		: 'tag',
					     		   	fieldLabel: 'Tag',
					     		   	width	: '98%'
					     	   }
					     	],
				buttons	 :	[
				       	  	 	{
				       	  	 		xtype	: 'button',
				       	  	 		id		: 'saveButton',
				       	  	 		text	: 'Upload',
				       	  	 		width	: 60,
				       	  	 		handler	: function(){
				       	  	 						uploadDocument();
				       	  	 						//showDocList();
				       	  	 					}
				       	  	 	},
				       	  	 	{
				       	  	 		xtype	: 'button',
				       	  	 		id		: 'resetButton',
				       	  	 		text	: 'Reset',
				       	  	 		width	: 60,
				       	  	 		handler	: function(){
				       	  	 					 alert("Hello");
				       	  	 					}
				       	  	 	}
				       		]
		};
		
		var mainUploadPanel = new Ext.FormPanel({
            id          : 'mainUploadPanel',
            autoHeight  : true,
            autoWidth   : true,
            fileUpload	: true,
            frame		: true,
            items       : [detailsPanel, 
                           //fileUploadGrid
                           CRM.commons.createGrid('uploadDocGrid', uploadDocStore, CRM.SPComp.createUploadDocColumnModel())
                           ],
            bbar        : {
			                xtype       : 'toolbar',
			                buttonAlign : 'right',
			                width		: 600,
			                items       : [
			                               '-',
			                               {
			                                xtype   : 'button',
			                                id		: 'closeButton',
			                                text    : 'Close',
			                                handler	: closeUploadWindow
			                               },
			                               '-'
			                            ]
            		}
        });      
		
		win = new Ext.Window({
	    	title		: 'Upload Document',
	    	id			: 'uploadWindow',
	    	autoWidth	: true,
	    	autoHeight	: true,
	    	border		: true,
	    	plain		: false,
	    	closable	: true,
	    	collapsible : true,
	    	animCollapse: true,
	    	draggable 	: true,
	    	maximizable : false,
	    	//layout	: 'border',
	    	onEsc		: true,
	    	model		: true,
	    	items		: [mainUploadPanel]
			});
		
	    win.show(this);
	    return win;
	};
	
	function closeUploadWindow(){
		win.close();
	};
	
	function showDocList(uploadDocGrid, selectedLeadId){
		var grid = Ext.getCmp(uploadDocGrid);
		grid.store.removeAll();
		grid.store.reload({
			params : {
						leadId : selectedLeadId
					 }
		});
	}
	
	function addRow(){
		var Document = Ext.data.Record.create([
			{
			    name: 'documentName'
			}, 
			{
			    name: 'receiveDate'
			},
			{
			    name: 'fileName'
			}
		]);
		
		var docGrid = Ext.getCmp('uploadDocGrid');
		docGrid.store.removeAll();
		alert('Hello');
		docGrid.store.add(new Document({
			documentName : 'CERTIFICATE',
			receiveDate  : '05/05/2011',
			fileName	 : 'CERTIFICATE.DOC'
		}));
		alert('Hi');
	}
	
    function uploadDocument() {
    	
    	var record = Ext.getCmp('spSearchLeadGridPanel').getSelectionModel().getSelected();
		var selectedLeadId = record.get('id');
    	
        var formPanel = Ext.getCmp('mainUploadPanel');
        if(formPanel.getForm().isValid()){
        	formPanel.getForm().submit({
	        	   enctype	: 'multipart/form-data',	
	        	   url		: 'documents/uploadDocument?leadId='+selectedLeadId,
	        	   method	: 'POST', 
	        	   waitMsg	: 'Uploading Document...',
	        	   success 	: function(formPanel, o){ 
	        		   			Ext.Msg.show({
									title: 'Result',
									msg: o.result.message,
									buttons: Ext.Msg.OK,
									icon: Ext.Msg.INFO
								});
	        		   			var docGrid = Ext.getCmp('uploadDocGrid');
	        		   			var record = Ext.getCmp('spSearchLeadGridPanel').getSelectionModel().getSelected();
	        		   			var selectedLeadId = record.get('id');
	        		   			showDocList(docGrid, selectedLeadId);
       	   					 },
	        	   failure 	: function(){ 
	        		   			alert ("Process Failure");
	        		   		}
	        	});	        	
        }
    };
})();